function deleteLine(){
	if(confirm(extension.confirmdel)){
	var line_id= $("#line_id").val();
	var settext = "&line_id="+line_id+"&id401=";
	submitform(modules["set_data"],settext);
	return true;
	}else{
		return false;
		}
}
function deleteTrunk(){
	if(confirm(trunk.confirmdel)){
	var line_id= $("#line_id").val();
	var settext = "&line_id="+line_id+"&id401=";
	submitform(modules["set_data"],settext);
	return true;
	}else{
		return false;
		}
}

function getSelectList(sendtext) {
    xmlDataRequest("get_data", sendtext, function (data) {
        var groupList = '';
        var ivrList = '<option value="">' + trunk.attendent + '</option>';
        $(data).find("item").each(function () {
            if ($(this).attr("id") == "4000" || $(this).attr("id") == "4001" || $(this).attr("id") == "4020" || $(this).attr("id") == "4021") {
                var selectList = '';
                var addLine = "";
                var nums = 0;
                var linearray = new Array();
                $(data).find("value").each(function () {
                    if ($.trim($(this).text().split("/")[2]) !== "") {
                        selectList += '<option value=' + $.trim($(this).text().split("/")[1]) + '>' + $.trim($(this).text().split("/")[0]) + '</option>';
                    } else {
                        addLine += '<option value=' + $.trim($(this).text().split("/")[1]) + '>' + $.trim($(this).text().split("/")[0]) + '</option>';
                    }
                    nums++;
                    linearray[nums] = $.trim($(this).text().split("/")[1]);
                });
                if (nums == 2 && $(this).attr("id") == "4000") {
                    $("#linetype").css("background", "url(../images/line2.png) center left no-repeat");
                    $("#linetype_title").html("PHONE1");
                    $("#linetype").html("<div style=\"width:50px;height:20px;margin-left:2px;margin-top:40px;float:left;\"><input type=\"button\" name=\"line\" value=\"PHONE2\" class=\"line_button\" onclick=\"getLinePage(" + linearray[2] + ");$('#linetype_title').html('PHONE2');setCookie( 'line_id'," + linearray[2] + ");\"/></div><div style=\"width:40px;height:20px;margin-left:2px;margin-top:40px;float:left;\"><input type=\"button\" name=\"line\" value=\"PHONE1\" class=\"line_button\" onclick=\"getLinePage(" + linearray[1] + ");$('#linetype_title').html('PHONE1');setCookie('line_id'," + linearray[1] + ");\"/></div>");
                } else if (nums == 1 && $(this).attr("id") == "4000") {
                    $("#linetype").css("background", "url(../images/line1.png) center left no-repeat");
                    $("#linetype_title").html("PHONE");
                    $("#linetype").html("<div style=\"width:50px;height:20px;margin-left:2px;margin-top:40px;float:left;\"></div><div style=\"width:40px;height:20px;margin-left:2px;margin-top:40px;float:left;\"><input type=\"button\" name=\"line\" value=\"PHONE\" class=\"line_button\"  onclick=\"getLinePage(" + linearray[1] + ");setCookie('line_id'," + linearray[1] + ");\"/></div>");
                }

                if (nums != 0 && ($(this).attr("id") == "4000" || $(this).attr("id") == "4001")) {

                        var line_id = linearray[1];
                        if (line_id) {
                            getLinePage(line_id);
                        }
                        setCookie("line_id", line_id);

                }

                $("#line_id").html(selectList);
                $("#startline").html(addLine);
                if (selectList == "" && $(this).attr("id") == "4000") {
                  $("#ip_extension_main_load").click();
                } else if (selectList == "" && $(this).attr("id") == "4001") {
                     $("#trunk").hide();
                     $("#ip_trunk_main_load").click();
                }
            } else if ($(this).attr("id") == "758" && $(this).attr("value") !== "") {
                groupList += '<option value="' + $(this).attr("line_id") + '">' + $(this).attr("value") + '</option>';
            } else if ($(this).attr("id") == "771") {
                var listarray = $(this).attr("value").split("|");
                var ring = new Array();
                var n = 0;
                for (var s = 0; s < listarray.length; s++) {
                    if (listarray[s].indexOf("user") >= 0 || listarray[s].indexOf("welcome") >= 0) {
                        ring[n] = listarray[s].split(".")[0];
                        n++;
                    }
                }
                var truearray = unique(ring);
                for (var i = 0; i < truearray.length; i++) {
                    ivrList += '<option>' + truearray[i] + '</option>';
                }
                $("#id468").html(ivrList);
            }
        });
        $("#id653").html(groupList);
        $("#myLineButton").val("LINE");
    });
}

function getLinePage(lineid){
	var sendtext = "&line_id="+lineid;
	var nonames = new Array("line_type","submit","extension_batch","extension_stat","add_extension","ip_extension_batch","ip_extension_stat","id366","id584","delete_extension","line");
	DataRequest("get_json",sendtext,function(data){
  		$(":input").each(function(){
			if(checkname(nonames,$(this).attr('name'))) {
				updatecfgvalue($(this).attr('name'),data[$(this).attr('name')]);
			}
		});
		/*if($("#id447_1").attr("name")){
			if(data["id447"]!=""){
				$("#s_447_1").html(trunk.outbound_option4);
				$("#s_447_2").html(trunk.outbound_option3);
			}else{
				$("#s_447_1").html(trunk.outbound_option1);
				$("#s_447_2").html(trunk.outbound_option2);
			}
		}
		*/
		
		if($("#id366").attr("name")){
			var thisvalue='';
			if(data["id366"]=="0"&&data["id600"]=="0") {
				thisvalue=0;
			}else if(data["id366"]=="1"&&data["id600"]=="0") {
				thisvalue=1;
			}else if(data["id366"]=="2"&&data["id600"]=="0") {
				thisvalue=2;
			}else if(data["id366"]=="0"&&data["id600"]=="1") {
				thisvalue=3;
			}else if(data["id366"]=="0"&&data["id600"]=="2") {
				thisvalue=4;
	  	 	}
	  	 	listselect("id366",thisvalue);
	 	}
	 	
	 	if($("#id469").attr("name")){
	 		if(data["id469"]==""){
				listselect("line_type",0);
			}else{
				listselect("line_type",1);
			}
			readtype();
	 		}
	 	
	 	if($("#id584").attr("name")){
	 		var thisvalue='';
	   		if(data["id447"]=="on") {
				thisvalue=data["id447"];
			}else {
				thisvalue=data["id584"];
			}
			listselect("id584",thisvalue);
		}
		if(read()){
			read();
		}
  	});
}

function changeSelect(boxId){
	var line_id = $("#"+boxId).val();
	getLinePage(line_id);
}
/**
 * map对象（键值对形式，目前值设置为2个，即一个key对应着2个val（有利于一些额外的需求））
 * 添加键值对的方法为：put（key,val1,val2）；
 * 取得第一个值：getValue（key）；
 * 取得第二个值：getReg（key）；（这个方法名字起的有点怪异，是因为我为了下面的json方法而设定的，但使用起来没有任何影响）
 * 判断是否为空：isEmpty（）；
 * 获取长度：size（）；
 * 获得key的集合：keySet（）；
 * 获得第一个值的集合：valueSet（）；
 * */
var Map = function() {
    this.isMap = true;
    var struct = function (key, value, other) {
        this.key = key;
        this.value = value;
        this.other = other;
    };
    var put = function (key, value, other) {
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key === key) {
                if (typeof other == Boolean || other === true) {
                    this.arr[i].value += "," + value;
                } else {
                    this.arr[i].value = value;
                    this.arr[i].other = other;
                }
                return;
            }
        }
        this.arr[this.arr.length] = new struct(key, value, other);
        return this;
    };

    var getOtherVal = function (key) {
        for (var i = 0; i < this.arr.length; i++)  if (this.arr[i].key === key) return this.arr[i].other;
        return null;
    };

    var getValue = function (key) {
        for (var i = 0; i < this.arr.length; i++)  if (this.arr[i].key === key) return this.arr[i].value;
        return null;
    };

    var remove = function (key) {
        var v;
        for (var i = 0; i < this.arr.length; i++) {
            v = this.arr.pop();
            if (v.key === key) continue;
            this.arr.unshift(v);
        }
    };

    var removeAll = function() {
        for (var a = 0; a < this.arr.length; a++) {
            var _key = this.arr[a].key;
            var v;
            for (var i = 0; i < this.arr.length; i++) {
                v = this.arr.pop();
                if (v.key === _key) continue;
                this.arr.unshift(v);
            }
        }
    };

    var keySet = function () {
        var keyArr = [];
        for (var i = 0; i < this.arr.length; i++) {
            var _key = this.arr[i].key;
            keyArr[i] = _key;
        }
        return keyArr;
    };

    var valSet = function () {
        var valArr = [];
        for (var i = 0; i < this.arr.length; i++) {
            var _val = this.arr[i].value;
            valArr[i] = _val;
        }
        return valArr;
    };

    var alertKeyAndVal = function () {
        var store = "";
        for (var i = 0; i < this.arr.length; i++) {
            var _val = this.arr[i].key;
            var _key = this.arr[i].value;
            store += "key:" + _key + ",val:" + _val;
        }
        return store;
    };
    var size = function () {
        return this.arr.length;
    };
    var isEmpty = function () {
        return this.arr.length <= 0;
    };

    this.arr = new Array();
    this.getValue = getValue;
    this.getOtherVal = getOtherVal;
    this.put = put;
    this.remove = remove;
    this.size = size;
    this.removeAll = removeAll;
    this.isEmpty = isEmpty;
    this.keySet = keySet;
    this.valSet = valSet;
    this.alertKeyAndVal = alertKeyAndVal;
};
